// src/pages/catering/goodsList/cartList/cartList.js
import getIn from '../../../../../yali-sdk/utils/getIn';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    selectPro: {
      type: Object,
      value: {},
      observer(newVal, oldVal) {
        if (newVal) {
          this.fetchData(newVal);
        }
      }
    },
    showAttr: {
      type: Boolean,
      value: false,
      observer(newVal, oldVal) {
        if (newVal) {
          this.showModal();
        }
      },
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    product_attr_group: {},
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**弹出优惠券弹窗**/
    showModal() {
      this.timers && clearTimeout(this.timers);
      const animation = wx.createAnimation({
        duration: 200,
        timingFunction: 'linear',
        delay: 0,
      });
      this.animation = animation;
      animation.scale(0).top('50%').left('10%').right('10%').bottom('auto').step();
      this.setData({ animationData: animation.export() });
      this.times = setTimeout(() => {
        animation.scale(1).top('50%').left('10%').right('10%').bottom('auto').step();
        this.setData({
          animationData: animation.export(),
        });
      }, 200);
    },
    // 取消弹出框
    hideModal() {
      this.timers && clearTimeout(this.timers);
      const animation = wx.createAnimation({
        duration: 200,
        timingFunction: 'linear',
        delay: 0,
      });
      this.animation = animation;
      animation.scale(0).top('50%').left('10%').right('10%').bottom('auto').step();
      this.setData({ animationData: animation.export() });
      this.times = setTimeout(() => {
        animation.scale(1).top('50%').left('10%').right('10%').bottom('auto').step();
        this.setData({
          animationData: animation.export(),
          product_attr_group:{}
        });
        this.triggerEvent('hideAttrBoxCallBack', { showAttr: false })
      }, 200);
    },
    // 处理数据
    fetchData(selectPro) {
      const { product_attr_group } = this.data;
      let obj = {};
      if (!product_attr_group.id) {
        obj = selectPro.product_attr_group ? selectPro.product_attr_group[0] : {};
      } else {
        obj = selectPro.product_attr_group.find(x => x.id == product_attr_group.id);
      }
      this.setData({ selectPro, product_attr_group: obj });
    },
    // 选择属性
    onSelect(e) {
      const { selectPro } = this.data;
      const group_attr = e.currentTarget.id;
      const product_attr_group = selectPro && selectPro.product_attr_group.find(x => x.id == group_attr);
      this.setData({ product_attr_group  });
    },
    // 修改购物车数据
    modifiesShopCart(e) {
      const num = getIn(e.currentTarget.dataset, ['num']);
      const group_attr = this.data.product_attr_group.id;
      this.triggerEvent('modifiesCart', { id: e.currentTarget.id, num, group_attr });
    },
    // 
  }
})
